    package classfile

import (
	"fmt"
	. "jvm/typedef"
)

type memberInfo struct {
	constantPool    ConstantPool
	accessFlags     U2
	nameIndex       U2
	descriptorIndex U2
	attributes      []Attribute
}
func (self *memberInfo) Print () {
	cp := self.constantPool
	fmt.Printf("access_flags:%d\n", self.accessFlags)
	fmt.Printf("name_index:#%d\t\t%s\n", self.nameIndex, cp.getUtf8(self.nameIndex))
	fmt.Printf("descriptor_index:#%d\t\t%s\n", self.descriptorIndex, cp.getUtf8(self.descriptorIndex))
	fmt.Printf("attributes:#%d\n", len(self.attributes))
	for index, val := range self.attributes {
		fmt.Printf("#%d{", index)
		val.Print()
		fmt.Println("}")
	}
}
func newFields(reader *BytecodeReader, constantPool ConstantPool) []Field {
	count := reader.readU2()
	fields := make([]Field, count)
	for i := 0; i < int(count); i++ {
		fields[i].constantPool = constantPool
		fields[i].readInfo(reader)
	}
	return fields
}
func newMethods(reader *BytecodeReader, constantPool ConstantPool) []Method {
	count := reader.readU2()
	methods := make([]Method, count)
	for i := 0; i < int(count); i++ {
		methods[i].constantPool = constantPool
		methods[i].readInfo(reader)
	}
	return methods
}

func (self *memberInfo) readInfo(reader *BytecodeReader) {
	self.accessFlags = reader.readU2()
	self.nameIndex = reader.readU2()
	self.descriptorIndex = reader.readU2()
	attributesCount := reader.readU2()
	self.attributes = make([]Attribute, attributesCount)
	for i := 0; i < int(attributesCount); i++ {
		attributeNameIndex := reader.readU2()
		attributeName := self.constantPool.getUtf8(attributeNameIndex)
		self.attributes[i] = newAttribute(attributeName, &self.constantPool)
		self.attributes[i].readInfo(reader)
	}
}

type Field struct {
	memberInfo
}

type Method struct {
	memberInfo
}
